package online.hxstart.code.concurrent.chapter03;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @author huxing
 * @date 19-4-23 星期二 下午7:15
 * @describe
 */
public class FightQueryTask extends Thread implements FightQuery {

  private final String origin;
  private final String destination;
  private final List<String> flightList = new ArrayList<>();

  public FightQueryTask(String airline, String origin, String destination) {
    super("[" + airline + "]");
    this.origin = origin;
    this.destination = destination;
  }

  @Override
  public List<String> get() {
    return flightList;
  }

  @Override
  public void run() {
    System.out.printf("%s-query from %s to %s \n", getName(), origin, destination);
    int randomVal = ThreadLocalRandom.current().nextInt(10);
    try {
      TimeUnit.SECONDS.sleep(randomVal);
      this.flightList.add(getName() + "-" + randomVal);
      System.out.printf("The Fight : %s list query successful \n", getName());
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
